<?

  $profiler_start = microtime();

  ob_start('ob_gzhandler');
  
  ob_start();
  
  include_once('lib/genlib.php');
  include_once('etc/config.php');
  include_once('lib/agcontroller.class.php');
  include_once('lib/agmodel.class.php');

  $srvp = $_SERVER['PATH_INFO'];

  $currentSiteId = 1; 
  
  $startupErrors = ob_get_clean();
  
  ob_start();

  $controller = getDefault(trim(strtolower($_REQUEST['controller'])), $config['site.defaultcontroller']);
  $_REQUEST['controller'] = $controller;
  
  $action = getDefault(trim(strtolower($_REQUEST['action'])), $config['site.defaultaction']);
  $_REQUEST['action'] = $action;
  
  foreach($config['include'] as $includefile)
    if(file_exists('custom/'.$includefile)) 
      include('custom/'.$includefile);
    else if(file_exists('lib/'.$includefile)) 
      include('lib/'.$includefile);
    else
      logError('cannot find include file: "'.$includefile.'"');
  
  AGController::invokeController($controller, $action, $config['loadmodel']);

  $content = $startupErrors.ob_get_clean();
  
  $doctitle = getDefault($config['doctitle'], $config['pagetitle']);
  $doctitle = getDefault($doctitle, $config['site.name']);
  
  if ($config['site.pagetemplate'] == 'blank')
    print($content);
  else
    include('themes/'.$config['site.theme'].'/page.'.$config['site.pagetemplate'].'.php');

  #logBackendError('requests', $_REQUEST);

?>
